﻿using ADRcpLib;
using ADSioLib;
using Common.Share;
using log4net;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO.Ports;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Media3D;
using System.Windows.Threading;
using VirtualWall.Machines;
using VirtualWall.Models;
using Camera = VirtualWall.Machines.Camera;

namespace VirtualWall
{
    public class FlowControl
    {
        #region 相关属性
        private static readonly ILog log = LogManager.GetLogger("VirtualWall");

        public static FlowControl _Instance;
        public static FlowControl Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new FlowControl();
                }
                return _Instance;
            }
        }

        public List<Camera> Cameras = new List<Camera>();
        public List<Rcp> Rcps = new List<Rcp>();

        #endregion

        private FlowControl() { }

        ///尝试启动逻辑
        public void TryRun()
        {
            //根据启动时传递的参数，决定初始化远程刷卡器或者车牌识别相机
            var m_Mode = ConfigurationManager.AppSettings["VirtualWall"];

            //初始化第三方平台
            Common.Platform.PlatformManager.LOGGER = log;
            Common.Platform.PlatformManager.Instance.Init("Default");
            //初始化结束

            switch (m_Mode)
            {
                case "1"://高频读头
                    {
                        InitSioBase();
                    }
                    break;
                case "2"://高频读头+ 车牌识别
                    {
                        //必须要先加载相机，因为下面的高频读头会依赖相机
                        InitCamera();
                        InitSioBase();
                    }
                    break;
            }
            log.Info("电子围栏启动");
        }

        #region 车牌识别相机

        /// <summary>
        /// 初始化加载拍照识别相机
        /// </summary>
        /// <returns></returns>
        private void InitCamera()
        {
            try
            {
                //初始化，第一步必须要调用的
                VzClientSDK.VzLPRClient_Setup();

                //相机1 初始化
                if (!string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["VWCamera1IP"]))
                {
                    var cameraIP = ConfigurationManager.AppSettings["VWCamera1IP"];
                    var cameraUser = ConfigurationManager.AppSettings["VWCamera1Username"];
                    var cameraPwd = ConfigurationManager.AppSettings["VWCamera1Password"];

                    int index = 1;
                    Camera camera = new Camera(CameraType.入口, index, cameraIP, cameraUser, cameraPwd, 80);
                    if (!Cameras.Any(a => a.Number == camera.Number))
                    {
                        Cameras.Add(camera);
                    }
                }

                //相机2 初始化
                if (!string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["VWCamera2IP"]))
                {
                    var cameraIP = ConfigurationManager.AppSettings["VWCamera2IP"];
                    var cameraUser = ConfigurationManager.AppSettings["VWCamera2Username"];
                    var cameraPwd = ConfigurationManager.AppSettings["VWCamera2Password"];

                    int index = 2;
                    Camera camera = new Camera(CameraType.出口, index, cameraIP, cameraUser, cameraPwd, 80);
                    if (!Cameras.Any(a => a.Number == camera.Number))
                    {
                        Cameras.Add(camera);
                    }
                }

                //相机3 初始化
                if (!string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["VWCamera3IP"]))
                {
                    var cameraIP = ConfigurationManager.AppSettings["VWCamera3IP"];
                    var cameraUser = ConfigurationManager.AppSettings["VWCamera3Username"];
                    var cameraPwd = ConfigurationManager.AppSettings["VWCamera3Password"];

                    int index = 3;
                    Camera camera = new Camera(CameraType.入口, index, cameraIP, cameraUser, cameraPwd, 80);
                    if (!Cameras.Any(a => a.Number == camera.Number))
                    {
                        Cameras.Add(camera);
                    }
                }

                //相机4 初始化
                if (!string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["VWCamera4IP"]))
                {
                    var cameraIP = ConfigurationManager.AppSettings["VWCamera4IP"];
                    var cameraUser = ConfigurationManager.AppSettings["VWCamera4Username"];
                    var cameraPwd = ConfigurationManager.AppSettings["VWCamera4Password"];

                    int index = 4;
                    Camera camera = new Camera(CameraType.出口, index, cameraIP, cameraUser, cameraPwd, 80);
                    if (!Cameras.Any(a => a.Number == camera.Number))
                    {
                        Cameras.Add(camera);
                    }
                }
            }
            catch (Exception e)
            {
                log.Error($"InitCamera Failed!{e.Message}");
            }
        }

        /// <summary>
        /// 初始化加载高频读头
        /// </summary>
        private void InitSioBase()
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["VWRF1Ip"]))
                {
                    int index = 1;
                    var carmera = Cameras.Find(p => p.Number == index);
                    if (carmera != null)
                    {
                        Rcp rcp = new Rcp(CameraType.入口, index, carmera, ConfigurationManager.AppSettings["VWRF1Ip"], 49152);

                        if (!Rcps.Any(a => a.Number == rcp.Number))
                        {
                            Rcps.Add(rcp);
                        }
                    }
                }

                if (!string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["VWRF2Ip"]))
                {
                    int index = 2;
                    var carmera = Cameras.Find(p => p.Number == index);
                    if (carmera != null)
                    {
                        Rcp rcp = new Rcp(CameraType.出口, index, carmera, ConfigurationManager.AppSettings["VWRF2Ip"], 49152);

                        if (!Rcps.Any(a => a.Number == rcp.Number))
                        {
                            Rcps.Add(rcp);
                        }
                    }
                }

                if (!string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["VWRF3Ip"]))
                {
                    int index = 3;
                    var carmera = Cameras.Find(p => p.Number == index);
                    if (carmera != null)
                    {
                        Rcp rcp = new Rcp(CameraType.入口, index, carmera, ConfigurationManager.AppSettings["VWRF3Ip"], 49152);

                        if (!Rcps.Any(a => a.Number == rcp.Number))
                        {
                            Rcps.Add(rcp);
                        }
                    }
                }

                if (!string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["VWRF4Ip"]))
                {
                    int index = 4;
                    var carmera = Cameras.Find(p => p.Number == index);
                    if (carmera != null)
                    {
                        Rcp rcp = new Rcp(CameraType.出口, index, carmera, ConfigurationManager.AppSettings["VWRF4Ip"], 49152);

                        if (!Rcps.Any(a => a.Number == rcp.Number))
                        {
                            Rcps.Add(rcp);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                log.Error($"InitSioBase Failed!{e.Message}");
            }
        }

        public void Dispose()
        {
            try
            {
                foreach (var camera in Cameras)
                {
                    camera.Dispose();
                }

                VzClientSDK.VZLPRClient_StopFindDevice();
                VzClientSDK.VzLPRClient_Cleanup();
            }
            catch(Exception e)
            {
                log.Error($"Dispose Error!{e.Message}");
            }
          
        }

        #endregion

    }
}
